<?php

//Load Master Page
require_once('../shared/layout.php');

//Load Shared Objects & Controls
require_once('../shared/alert.php');
require_once('../shared/confirmation.php');

class Index extends Layout {

    protected $objAlert, $dlgConfirmation;
    protected $dtgUser;
    protected $pxyDelete;

    protected function Form_Create() {
        parent::Form_Create();

        $this->strContextMenu = __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/user/menu/index.tpl.php';
        
        $this->objAlert = new Alert($this);

        $this->dlgConfirmation = new Confirmation($this, null, new QAjaxAction('dlgConfirmation_ConfirmAction'), null);

        $this->pxyDelete = new QControlProxy($this);
        $this->pxyDelete->AddAction(new QClickEvent(), new QAjaxAction('pxyDelete_Click'));
        $this->pxyDelete->AddAction(new QClickEvent(), new QTerminateAction());

        $this->dtgUser = new UserDataGrid($this);
        $this->dtgUser->AdditionalConditions = QQ::NotEqual(QQN::User()->IsAdmin, true);
        $this->dtgUser->ShowFilter = false;
        $this->dtgUser->ShowFooter = true;

        $this->dtgUser->Paginator = $this->dtgUser->PaginatorAlternate = new QPaginator($this->dtgUser);
        $this->dtgUser->ItemsPerPage = 10;

        $this->dtgUser->MetaAddColumn(QQN::User()->FirstName);
        $this->dtgUser->MetaAddColumn(QQN::User()->LastName);
        $this->dtgUser->MetaAddColumn(QQN::User()->IsEnable);
        $this->dtgUser->MetaAddEditLinkColumn('edit.php', 'Edit', 'text-info', null, QMetaControlArgumentType::QueryString);
        $this->dtgUser->MetaAddEditProxyColumn($this->pxyDelete, 'Delete', 'text-error', null);
    }

    public function pxyDelete_Click($strFormId, $strControlId, $strParameter) {

        $objUser = User::Load($strParameter);

        $this->dlgConfirmation->strTitle = QApplication::Translate('Delete User');
        $this->dlgConfirmation->strMessage = QApplication::Translate(sprintf('Do you want to delete "%s"?', $objUser->FullName));
        $this->dlgConfirmation->btnConfirm->ActionParameter = $objUser->Id;
        $this->dlgConfirmation->Show();
    }

    public function dlgConfirmation_ConfirmAction($strFormId, $strControlId, $strParameter) {

        try {
            $objUser = User::Load($strParameter);
            $objUser->Delete();

            $this->dtgUser->Refresh();
            $this->objAlert->Success(QApplication::Translate('Success'), QApplication::Translate(sprintf('User "%s" Deleted Successful', $objUser->FullName)));
            $this->dlgConfirmation->Hide();
        } catch (QCallerException $objExc) {
            $this->objAlert->Error(QApplication::Translate('Ooops!'), $objExc->getMessage());
        }
    }

}

Index::Run('Index', __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/user/index.tpl.php');
?>